﻿using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using UFIDA.U9.CBO.SCM.PickList;
using UFIDA.U9.CBO.SCM.Warehouse;
using UFIDA.U9.Cust.YYD.PlugBE.Dtos;
using UFIDA.U9.Cust.YYD.PlugBE.Public;
using UFSoft.UBF.Business;

namespace UFIDA.U9.Cust.YYD.PlugBE.PurchaseOrderP
{
    public class AfterInserting : UFSoft.UBF.Eventing.IEventSubscriber
    {
        public void Notify(params object[] args)
        {
            #region 从事件参数中取得当前业务实体													 
            if (args == null || args.Length == 0 || !(args[0] is UFSoft.UBF.Business.EntityEvent))
                return;
            BusinessEntity.EntityKey key = ((UFSoft.UBF.Business.EntityEvent)args[0]).EntityKey;
            if (key == null)
                return;
            var holder = key.GetEntity() as UFIDA.U9.PM.PO.PurchaseOrder;
            if (holder == null)
                return;
            #endregion
            if (holder.Org.Code == "1010")//深圳
            {
                using (ISession session = Session.Open())
                {
                    foreach (var line in holder.POLines)
                    {

                        foreach (var shipline in line.POShiplines)
                        {
                            shipline.Wh = line.ItemInfo.ItemID.InventoryInfo.Warehouse;
                        }


                        if (holder.DocumentType.BusinessType == Base.Doc.BusinessTypeEnum.PM055)
                        {
                            var SCMPickLists = SCMPickList.Finder.FindAll($"PicKHead = {line.SCMPickHead?.ID??0}");
                            foreach (var SCMPickList in SCMPickLists)
                            {
                                SCMPickList.SupplyWh = SCMPickList.ItemInfo.ItemID.InventoryInfo.Warehouse;
                            }
                        }


                    }
                    session.Commit();
                }

            }

            if (holder.Org.Code == "1020")//安徽
            {
                using (ISession session = Session.Open())
                {
                    foreach (var line in holder.POLines)
                    {
                        Warehouse wh = Warehouse.Finder.Find($"Code = '{line.ItemInfo.ItemID.DescFlexField.PrivateDescSeg8}' and Org = {holder.Org.ID}");
                        if (wh != null)
                        {

                            foreach (var shipline in line.POShiplines)
                            {
                                shipline.Wh = wh;
                            }

                            if (holder.DocumentType.BusinessType == Base.Doc.BusinessTypeEnum.PM055)
                            {
                                var SCMPickLists = SCMPickList.Finder.FindAll($"PicKHead = {line.SCMPickHead?.ID??0}");
                                foreach (var SCMPickList in SCMPickLists)
                                {
                                    Warehouse wh2 = Warehouse.Finder.Find($"Code = '{SCMPickList.ItemInfo.ItemID.DescFlexField.PrivateDescSeg8}' and Org = {holder.Org.ID}");
                                    if (wh2 != null)
                                    {
                                        SCMPickList.SupplyWh = wh2;
                                    }
                                }
                            }

                        }
                        session.Commit();
                    }
                }
            }

        }
    }

}
